﻿Imports System.Runtime.Serialization
''' <summary>
''' Construit un DataSet a partir des Meta qui sont linker ensemble.
''' </summary>
''' <remarks>On pourrait mettre des MetaDataView a la place des Metadatatable...</remarks>
Public Class MetaDataSet
    Inherits DataSet

    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal datasetName As String)
        MyBase.New(datasetName)
    End Sub
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext, ByVal constructSchema As Boolean)
        MyBase.New(info, context, constructSchema)
    End Sub

    Public Property QueryName As String = Nothing
    Public Property ControlID As Guid = Guid.Empty
    Public Property QueryID As Guid = Guid.Empty
    Public Property SQLWhere As String

    Private mq As MetaQuery
    Private mainDataTable As MetaDataTable

    Public Sub FillDataSetByQueryName()
        If QueryName IsNot Nothing Then
            mq = MetaQuery.GetMetaQueryByName(QueryName)
            If mq Is Nothing Then
                Throw New Exception(String.Format("MetaCommand for {0} does not exist", QueryName))
            Else
                InitData()
            End If
        Else
            Throw New Exception("Queryname must be initialize")
        End If
    End Sub

    Public Sub FillDataSetByControlID()
        If Not ControlID.Equals(Guid.Empty) Then
            mq = MetaQuery.GetMetaQueryByControlID(ControlID)
            If mq Is Nothing Then
                Throw New Exception(String.Format("MetaCommand for MetaControl guid {0} does not exist", ControlID.ToString))
            Else
                QueryName = mq.QueryName
                InitData()
            End If
        Else
            Throw New Exception("QueryID must be initialize")
        End If
    End Sub

    Public Sub FillDataSetByQueryID()
        If Not QueryID.Equals(Guid.Empty) Then
            mq = MetaQuery.GetMetaQueryById(QueryID)
            If mq Is Nothing Then
                Throw New Exception(String.Format("MetaCommand for guid {0} does not exist", QueryID.ToString))
            Else
                QueryName = mq.QueryName
                InitData()
            End If
        Else
            Throw New Exception("QueryID must be initialize")
        End If
    End Sub

    Public Overridable Sub InitData()
        Dim aChildRelationField As String
        Dim DataViewAccess As MetaQueryAccess
        Dim aDR As DataRelation
        Dim metaDataTableChild As CommonExt.MetaDataTable
        Dim childQuery As String

        Try
            Me.mainDataTable = New MetaDataTable()
            Me.mainDataTable.QueryName = Me.QueryName
            If Not String.IsNullOrEmpty(Me.SQLWhere) Then
                Me.mainDataTable.GetMetaCommand()
                If Not String.IsNullOrEmpty(Me.mainDataTable.MetaCommand.SQLWhere) Then
                    Me.mainDataTable.MetaCommand.SQLWhere += " AND "
                End If
                Me.mainDataTable.MetaCommand.SQLWhere += String.Format(" {0}", Me.SQLWhere)
            Else
                Me.mainDataTable.GetMetaCommand()
            End If
            'TestPoint: Get the SqlStatement in immediate widow with, ?Me.MetaDataTable.MetaCommand.SQLStatement
            Me.mainDataTable.FillBusinessApplicationData()
            'on assigne la meta Sécurity des MetaQuery définie par les MetaControls sur la dataview 
            DataViewAccess = New MetaQueryAccess(mainDataTable.MetaCommand.MetaQuerieCD)
            'Testpoint: if security is not define properly in metacontrol the MetaQueryAccess always return FALSE for all of it's properties by design B.Desjardins 2012-05-16...
            mainDataTable.DefaultView.AllowNew = DataViewAccess.AllowCreate
            mainDataTable.DefaultView.AllowEdit = DataViewAccess.AllowUpdate
            mainDataTable.DefaultView.AllowDelete = DataViewAccess.AllowDelete
            'oustanding: y reste le allowRead ,BD 2012-05-16

            Me.Tables.Add(Me.mainDataTable)
            For Each row As DataRow In Me.mainDataTable.MetaCommand.GetChildDisplayQuery.Rows
                If CInt(row("RelationOrder")) >= 0 Then
                    childQuery = row("QueryName").ToString()
                    'aChildRelationField = String.Format("{0}_FK", row("ParentGUIDField").ToString)
                    aChildRelationField = row("FKColumn_Name").ToString

                    metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
                    'Ici on chaine nos WHERE Clause, pour que les enfants soit scopper sur le parent
                    '
                    metaDataTableChild.GetMetaCommand()
                    DataViewAccess = New MetaQueryAccess(metaDataTableChild.MetaCommand.MetaQuerieCD)
                    'if the control is define and you have access
                    If DataViewAccess.AllowRead = CShort(False) Then
                        metaDataTableChild.MetaCommand.SQLWhere = "1=2"
                    Else
                        If Not String.IsNullOrEmpty(Me.SQLWhere) Then
                            If Not String.IsNullOrEmpty(metaDataTableChild.MetaCommand.SQLWhere) Then
                                metaDataTableChild.MetaCommand.SQLWhere += " AND "
                            End If
                            metaDataTableChild.MetaCommand.SQLWhere += String.Format(" {0} IN ({1})", row("FKColumn_Name").ToString, Me.mainDataTable.MetaCommand.GetSQLForSubSelect(row("PKColumn_Name").ToString))
                        End If
                    End If

                    metaDataTableChild.FillBusinessApplicationData()
                    metaDataTableChild.DefaultView.AllowNew = DataViewAccess.AllowCreate
                    metaDataTableChild.DefaultView.AllowEdit = DataViewAccess.AllowUpdate
                    metaDataTableChild.DefaultView.AllowDelete = DataViewAccess.AllowDelete

                    Me.Tables.Add(metaDataTableChild)
                    aDR = New DataRelation(row("Relation").ToString,
                                            Me.Tables(row("ParentQueryName").ToString).Columns(row("PKColumn_Name").ToString),
                                            metaDataTableChild.Columns(aChildRelationField),
                                            True)
                    Me.Relations.Add(aDR)
                End If
            Next
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class
